from pymodelica import compile_fmu
import sys


def compile_model(model_name, jvm_args, compile_to):
    try:
        fmu_path = compile_fmu(model_name,
                               compiler_options={
                                   "copy_source_files_to_fmu": True,
                                   "max_n_proc": 1,
                                   "log_level": 0,
                                   "use_jacobian_equilibration": True,
                                   "enable_variable_scaling": True
                               },
                               target='cs',
                               jvm_args=jvm_args,
                               compile_to=compile_to)
        print("Successfully compiled FMU for model {}: {}".format(model_name, fmu_path))
    except Exception as e:
        print("Failed to compile FMU for model {}. Error: {}".format(model_name, e))


if __name__ == '__main__':
    model_name = sys.argv[1]
    jvm_args = sys.argv[2]
    compile_to = sys.argv[3]
    compile_model(model_name, jvm_args, compile_to)
